﻿update = 0;                //更新数据标志 1：更新音量大小

var page = {
    urlList: 0,             //播放url路径
    playIndex: 0,           //音乐列表索引
    albumData: 0,           //用于接收上界面传来的music—json
    isPlay: false,          //播放选项
    totalTime: 0,           //播放时长
    currentTime: 0,
    timer: 0,               //processbar timer
    process_data: 0,        //播放进度条    
    process_data2: 0,
    globalVolume: 20,       //音量初始值--滑块

    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {
        refreshDate(this);          //获取信号质量--app.js
        this.audioEvenDeinit();
        this.albumData = event;
        this.urlList = new Array(); //存放音乐数组
        var imageArr = this.albumData.albumImage.split("/");
        this.setData({ albumImage: imageArr[imageArr.length - 1] });
        this.setData({ albumInfo: this.albumData.albumName });
        this.setData({ volume_slider: { value: audio.getVolume() } });
        var listItems = new Array();
        for (var i = 0, len = this.albumData.albumList.length; i < len; i++) {
            var item = new Object();
            item.label1 = new Object();
            item.panel1 = new Object();
            item.panel1.id = "musicPanel" + i;      //设置每一个列表前的图标
            item.imagebox1 = new Object();
            item.imagebox1.id = "musicImage" + i;   //设置每一个列表前的图标id
            item.label1.value = this.albumData.albumList[i].trackName;//每一条音乐名称
            console.log("@@=======================TrackName=======================");
            console.log("@@=====>trackName is:" + this.albumData.albumList[i].trackName);//音乐名称
            /*** 路径 ***/
            this.urlList.push(this.albumData.albumList[i].src);
            console.log("@@=======================AlbumList_src=======================");
            console.log("@@=====>AlbumList is:" + this.albumData.albumList[i].src);//mp3文件
            listItems.push(item);
        }
        this.setData({
            List: {
                list: {
                    page: this,
                    items: [{
                        xml: "Panels/PlayListItem",
                        items: listItems
                    }]
                }
            }
        });
        this.audioEventInit();
    },

    //audio事件
    audioEventInit: function () {
        var that = this;
        audio.onPlay(function (e) {     //监听音频播放 e:时长
            that.isPlay = true;
            that.setData({ Switch1: { value: true } })
            that.totalTime = e;
        });
        audio.onPause(function (e) {    //监听音频暂停
            console.log("==> onPause : " + e);
        });
        audio.onEnded(function (e) {    //监听音频播放结束
            console.log("==> onEnded : " + e);
            that.setData({ Switch1: { value: false } });
            that.process_data = 0;
            if (wifi.isConnected) {
                that.playIndex += 1;    //下一曲
                that.playMusic();
            }
        });
        audio.onTimeUpdate(function (e) {//监听音频播放进度
            /*刷新进度条*/
            var time = parseInt(e / 1000);
            if (that.currentTime != time) {
                that.process_data = time;
                that.process_data2 = time;
                console.log("@@==>audio time:" + time);
                that.currentTime = time;
            }
        });
        audio.onError(function (e) {    //监听音频播放错误
            console.log("==> onError : " + e);
            audio.stop();
        });
        audio.onSeeked(function (e) {   //监听音频完成 seek 操作
            console.log("==> onSeeked : " + e);
        });
        audio.onStop(function (e) {     //监听音频停止
            console.log("==> onStop : " + e);
            that.setData({ Switch1: { value: false } });
            that.process_data = 0;      //刷新进度条
        });
        this.changeIndex(this.playIndex);//下一曲
    },

    //切换下一曲--列表下移动画
    changeIndex: function (targetId) {
        var data = new Object();
        data["musicImage" + targetId] = { value: "icon_playing.png" };
        console.log("@@====>musicImagedata:" + data["musicImage" + targetId] + "---line:95");
        this.setData(data);
        this.setData({ List: { move: 30 * targetId } })
    },

    onPlay: function (e) {
        var targetId = parseInt(e.target.id.substring(10));
        console.log("@@=====>onPlay is click---targetId:" + targetId + "---line:102");
        var data = new Object();
        data["musicImage" + this.playIndex] = { value: "icon_play.png" };
        this.setData(data);
        this.changeIndex(targetId);
        this.playIndex = targetId;
        if (this.isPlay) {
            this.playMusic();
        }
    },

    //播放音乐
    playMusic: function () {
        if (this.isPlay == false) {
            this.isPlay = true;
            this.setData({ Switch1: { value: true } })
        }
        if (this.playIndex < 0) {
            this.playIndex = this.urlList.length - 1;
        } else if (this.playIndex >= this.urlList.length) {
            this.playIndex = 0;
        }

        // set url
        var url = this.urlList[this.playIndex];
        console.log("@@url is===>" + url + "----line:137");
        audio.stop();
        audio.setSrc(url);
        if (this.isPlay && wifi.isConnected) {
            audio.play();
        } else {
            this.isPlay = false;
            this.setData({ Switch1: { value: false } });
        }
    },

    //暂停/播放--按钮回调
    onSwitch: function (event) {
        this.isPlay = event.detail.value;
        if (this.isPlay) {
            this.playMusic();
        } else {
            audio.pause();
        }
    },

    //切换歌曲--下一首/上一首--按钮回调
    onTap: function (event) {
        switch (event.target.id) {
            case "before":
                audio.stop();
                var data = new Object();
                data["musicImage" + this.playIndex] = { value: "icon_play.png" };
                this.setData(data);
                this.playIndex--;
                if (this.playIndex < 0) {
                    this.playIndex = 0;
                }
                this.changeIndex(this.playIndex);
                if (this.isPlay) {
                    this.playMusic();
                }
                break;
            case "next":
                audio.stop();
                var data = new Object();
                data["musicImage" + this.playIndex] = { value: "icon_play.png" };
                this.setData(data);
                this.playIndex++;
                if (this.playIndex >= this.urlList.length) {
                    this.playIndex = this.urlList.length - 1;
                }
                this.changeIndex(this.playIndex);
                if (this.isPlay) {
                    this.playMusic();
                }
                break;
        }
    },

    //滑动条改变音量
    onVolumeChange: function (event) {
        globalVolume = event.detail.value;
        update = 1;
    },

    //audio初始化
    audioEvenDeinit: function () {
        audio.offPlay();    //取消监听音频播放事件
        audio.offPause();   //取消监听音频暂停事件
        audio.offEnded();
        audio.offTimeUpdate();
        audio.offError();
        audio.offSeeked();
        audio.offStop();
        audio.stop();
    },

    //点击返回按钮回调
    onBack: function (event) {
        var obj = new Object();
        obj.page = "video";
        this.audioEvenDeinit();
        pm.navigateBack({ value: obj });
    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {
        var that = this;
        that.timer = setInterval(function () {
            if (update == 1) {
                audio.setVolume(globalVolume);
                update = 0;
            }
            if (that.process_data > that.process_data2 + 1) {
                that.setData({ progressbar1: { value: 0 } });
                that.process_data = 0;
            } else {
                /*更新progressbar的进度和label的显示文本*/
                that.setData({ progressbar1: { value: that.process_data } });
            }
        }, 200)
    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
        if (this.timer != 0) {
            clearInterval(this.timer);
        }
        if (this.dateTimer != 0) {
            clearInterval(this.dateTimer);
        }
    },
};

Page(page);

page = 0;
